﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	FormsFactory.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;

namespace Smart.Common.Forms
{
	/// <summary>
	/// Forms factory
	/// </summary>
	public class FormsFactory : IFormsFactory
	{
		public SPServicesInstance Services { get; set; }

		public FormsFactory(SPServicesInstance services)
		{
			Services = services;
		}

		/// <summary>
		/// Register forms service
		/// </summary>
		public static void Register<TFormsServiceType>(string serviceName)
			where TFormsServiceType : class, IFormsService, new()
		{
			SPServices.ServiceLocator.Register<IFormsService, TFormsServiceType>(serviceName);
		}

		/// <summary>
		/// Unregister forms service
		/// </summary>
		public static void Unregister(string serviceName)
		{
			SPServices.ServiceLocator.Unregister<IFormsService>(serviceName);
		}

		/// <summary>
		/// Returns forms service
		/// </summary>
		protected TFormsServiceType Get<TFormsServiceType>(string serviceName)
			where TFormsServiceType : class, IFormsService
		{
			return Services.ServiceLocator.GetInstance<TFormsServiceType>(serviceName);
		}

		/// <summary>
		/// Get forms service by serviceName
		/// </summary>
		public virtual IFormsService GetService(string serviceName)
		{
			var service = Get<IFormsService>(serviceName);

			if (service == null)
				throw new NotSupportedException();

			service.InitContext(serviceName);

			service.InitService();

			return service;
		}

		/// <summary>
		/// Build form for <paramref name="entity"/> in <paramref name="container"/>
		/// </summary>
		public virtual bool BuildForm(string serviceName, IFormEntity entity, IFormContainer container, MetaFormMode mode, IDictionary<string, object> values)
		{
			if (serviceName.IsNullOrEmpty())
				throw new ArgumentNullException("serviceName");

			if (entity == null)
				throw new ArgumentNullException("entity");

			if (container == null)
				throw new ArgumentNullException("container");

			var service = GetService(serviceName);

			return service.BuildForm(entity, container, mode, values);
		}

		/// <summary>
		/// Build form for <paramref name="entity"/> in <paramref name="container"/> and save entity values
		/// </summary>
		public virtual bool BuildFormAndSave(string serviceName, IFormEntity entity, IFormContainer container, MetaFormMode mode, IDictionary<string, object> values)
		{
			if (serviceName.IsNullOrEmpty())
				throw new ArgumentNullException("serviceName");

			if (entity == null)
				throw new ArgumentNullException("entity");

			if (container == null)
				throw new ArgumentNullException("container");

			var service = GetService(serviceName);

			return
				service.BuildForm(entity, container, mode, values)
				&& service.SaveForm();
		}
	}
}